package com.jingxuan.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jingxuan.common.result.Result;
import com.jingxuan.common.result.ResultCode;
import com.jingxuan.dto.request.AddCartItemRequest;
import com.jingxuan.dto.request.SetCartItemSelectedRequest;
import com.jingxuan.dto.request.UpdateCartItemQuantityRequest;
import com.jingxuan.dto.response.AddCartItemResponse;
import com.jingxuan.dto.response.ClearCartResponse;
import com.jingxuan.dto.response.GetCartItemResponse;
import com.jingxuan.dto.response.SetCartItemSelectedResponse;
import com.jingxuan.dto.response.UpdateCartItemQuantityResponse;
import com.jingxuan.interceptor.AuthInterceptor;
import com.jingxuan.service.CartService;

import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;

    /**
     * 添加商品到购物车
     * POST /cart
     */
    @PostMapping
    public Result<AddCartItemResponse> addCartItem(@Valid @RequestBody AddCartItemRequest request) {
        log.info("添加商品到购物车，请求参数：{}", request);
        Long userId = AuthInterceptor.getCurrentUserId();
        if (userId == null) {
            return Result.error(ResultCode.UNAUTHORIZED, "未登录，无法添加商品到购物车");
        }
        AddCartItemResponse response = cartService.addCartItem(userId, request);
        return Result.success("添加商品到购物车成功", response);
    }

    /**
     * 获取购物车列表
     * GET /cart
     */
    @GetMapping
    public Result<GetCartItemResponse> getCartItems() {
        log.info("获取购物车列表");
        Long userId = AuthInterceptor.getCurrentUserId();
        if (userId == null) {
            return Result.error(ResultCode.UNAUTHORIZED, "未登录，无法获取购物车列表");
        }
        GetCartItemResponse response = cartService.getCartItems(userId);
        return Result.success("获取购物车列表成功", response);
    }

    /**
     * 更新购物车商品数量
     * PUT /cart/{productId}
     */
    @PutMapping("/{productId}")
    public Result<UpdateCartItemQuantityResponse> updateCartItemQuantity(@PathVariable String productId,
                                                                          @Valid @RequestBody UpdateCartItemQuantityRequest request) {
        log.info("更新购物车商品数量，请求参数：{}", request);
        Long userId = AuthInterceptor.getCurrentUserId();
        if (userId == null) {
            return Result.error(ResultCode.UNAUTHORIZED, "未登录，无法更新购物车商品数量");
        }
        UpdateCartItemQuantityResponse response = cartService.updateCartItemQuantity(userId, productId, request.getQuantity());
        return Result.success("更新购物车商品数量成功", response);
    }

    /**
     * 是否选中购物车商品
     * PUT /cart/{productId}/selected
     */
    @PutMapping("/{productId}/selected")
    public Result<SetCartItemSelectedResponse> setCartItemSelected(@PathVariable String productId,
                                                                   @Valid @RequestBody SetCartItemSelectedRequest request) {
        log.info("是否选中购物车商品，请求参数：{}", request);
        Long userId = AuthInterceptor.getCurrentUserId();
        if (userId == null) {
            return Result.error(ResultCode.UNAUTHORIZED, "未登录，无法选中购物车商品");
        }
        SetCartItemSelectedResponse response = cartService.setCartItemSelected(userId, productId, request.getSelected());
        return Result.success("选中/取消选中购物车商品成功", response);
    }

    /**
     * 清空购物车
     * DELETE /cart
     */
    @DeleteMapping
    public Result<ClearCartResponse> clearCart() {
        log.info("清空购物车");
        Long userId = AuthInterceptor.getCurrentUserId();
        if (userId == null) {
            return Result.error(ResultCode.UNAUTHORIZED, "未登录，无法清空购物车");
        }
        ClearCartResponse response = cartService.clearCart(userId);
        return Result.success("清空购物车成功", response);
    }
}
